//
//  HomeTableViewController.m
//  bitcoin
//
//  Created by caiyajun on 2018/5/23.
//  Copyright © 2018年 比特币. All rights reserved.
//

#import "HomeTableViewController.h"
#import "SDCycleScrollView.h"
#import "HomeTableViewCell.h"
#import "BannerModel.h"
@interface HomeTableViewController ()<SDCycleScrollViewDelegate>

@property (weak, nonatomic) IBOutlet UIView *headerV;
@property (weak, nonatomic) IBOutlet SDCycleScrollView *bannerScrollView;
@property (nonatomic, strong) NSMutableArray *bannerArray;
@property (weak, nonatomic) IBOutlet UIView *lineView;
@property (weak, nonatomic) IBOutlet UIButton *riseBtn;
@property (weak, nonatomic) IBOutlet UIButton *fallBtn;
@property (weak, nonatomic) IBOutlet UIButton *eyeBtn;
@property (weak, nonatomic) IBOutlet UILabel *propertyLabel;
@property (weak, nonatomic) IBOutlet UILabel *hidePropertyLabel;
@property (weak, nonatomic) IBOutlet UIButton *loginBtn;

- (IBAction)loginAction;
@property (weak, nonatomic) IBOutlet UIView *loginView;
@property (weak, nonatomic) IBOutlet UIView *contentView;

@property (weak, nonatomic) IBOutlet UILabel *currencyRelationOneLabel;
@property (weak, nonatomic) IBOutlet UILabel *currencyRelationTwoLabel;
@property (weak, nonatomic) IBOutlet UILabel *currencyRelationThreeLabel;

@property (weak, nonatomic) IBOutlet UILabel *priceOneLabel;
@property (weak, nonatomic) IBOutlet UILabel *priceTwoLabel;
@property (weak, nonatomic) IBOutlet UILabel *priceThreeLabel;
@property (weak, nonatomic) IBOutlet UILabel *upsAndDownsOneLabel;
@property (weak, nonatomic) IBOutlet UILabel *upsAndDownsTwoLabel;
@property (weak, nonatomic) IBOutlet UILabel *upsAndDownsThreeLabel;

@end

@implementation HomeTableViewController
- (NSMutableArray *)bannerArray {
    if (!_bannerArray) {
        _bannerArray = [NSMutableArray array];
    }
    return _bannerArray;
}
- (void)viewDidLoad {
    [super viewDidLoad];
//    self.headerV.frame=CGRectMake(0, 0, 0, 300);
    
    
    self.bannerScrollView.delegate = self;
    self.bannerScrollView.backgroundColor=[UIColor clearColor];
//    NSArray *imageNames = @[@"h1.jpg",
//                            @"h1.jpg",
//                            @"h1.jpg",
//                            @"h1.jpg"// 本地图片请填写全名
//                            ];
//    self.bannerScrollView.localizationImageNamesGroup=imageNames;
    self.bannerScrollView.currentPageDotImage=[UIImage imageNamed:@"yellow"];
    self.bannerScrollView.pageDotImage=[UIImage imageNamed:@"gray"];
    
    
    [self.riseBtn setTitleColor:RGBColor(153, 153, 153) forState:UIControlStateNormal];
    [self.riseBtn setTitleColor:RGBColor(44, 178, 90) forState:UIControlStateSelected];
    self.riseBtn.selected=YES;
    
    [self.fallBtn setTitleColor:RGBColor(153, 153, 153) forState:UIControlStateNormal];
    [self.fallBtn setTitleColor:RGBColor(225, 69, 79) forState:UIControlStateSelected];
    self.fallBtn.selected=NO;
    
    [_eyeBtn setImage:[UIImage imageNamed:@"open-icon"] forState:UIControlStateNormal];
    [_eyeBtn setImage:[UIImage imageNamed:@"close-icon"] forState:UIControlStateSelected];
    self.hidePropertyLabel.hidden=YES;
    
    self.loginBtn.layer.borderColor = RGBColor(64, 69, 90).CGColor;
    self.loginBtn.layer.borderWidth = 1;
    //轮播图
    [self loadNewData];
    //滚动市场信息
    [self scrollMarket];
    
    
    
    
    
}
- (void)scrollMarket{
    [LSRequestManager getRequestWithURL:MARKET(@"scrollMarket")
                                 params:nil
                                success:^(NSDictionary *responseData) {
                                    if ([responseData[@"ret"] integerValue ] == 0) {
                                        NSArray*array=responseData[@"data"];
                                        if (array.count>2) {
                                            NSDictionary *dic = array[0];
                                            self.currencyRelationOneLabel.text=dic[@"currencyRelation"];
                                            self.priceOneLabel.text=dic[@"price"];
                                            self.upsAndDownsOneLabel.text=dic[@"upsAndDowns"];
                                            if ([self.upsAndDownsOneLabel.text containsString:@"-"]) {
                                                
                                                
                                                self.priceOneLabel.textColor=RGBColor(225, 69, 79);
                                                self.upsAndDownsOneLabel.textColor=RGBColor(225, 69, 79);
                                            } else {
                                                
                                                
                                                self.priceOneLabel.textColor=RGBColor(44, 178, 90);
                                                self.upsAndDownsOneLabel.textColor=RGBColor(44, 178, 90);
                                                
                                            }
                                            
                                            NSDictionary *dic1 = array[1];
                                            self.currencyRelationTwoLabel.text=dic1[@"currencyRelation"];
                                            self.priceTwoLabel.text=dic1[@"price"];
                                            self.upsAndDownsTwoLabel.text=dic1[@"upsAndDowns"];
                                            if ([self.upsAndDownsTwoLabel.text containsString:@"-"]) {
                                                
                                                
                                                self.priceTwoLabel.textColor=RGBColor(225, 69, 79);
                                                self.upsAndDownsTwoLabel.textColor=RGBColor(225, 69, 79);
                                            } else {
                                                
                                                
                                                self.priceTwoLabel.textColor=RGBColor(44, 178, 90);
                                                self.upsAndDownsTwoLabel.textColor=RGBColor(44, 178, 90);
                                                
                                            }
                                            
                                            NSDictionary *dic2 = array[2];
                                            self.currencyRelationThreeLabel.text=dic2[@"currencyRelation"];
                                            self.priceThreeLabel.text=dic2[@"price"];
                                            self.upsAndDownsThreeLabel.text=dic2[@"upsAndDowns"];
                                            if ([self.upsAndDownsThreeLabel.text containsString:@"-"]) {
                                                
                                                
                                                self.priceThreeLabel.textColor=RGBColor(225, 69, 79);
                                                self.upsAndDownsThreeLabel.textColor=RGBColor(225, 69, 79);
                                            } else {
                                                
                                                
                                                self.priceThreeLabel.textColor=RGBColor(44, 178, 90);
                                                self.upsAndDownsThreeLabel.textColor=RGBColor(44, 178, 90);
                                                
                                            }
                                        }else{
                                            
                                            self.currencyRelationOneLabel.text=@"";
                                            self.priceOneLabel.text=@"";
                                            self.upsAndDownsOneLabel.text=@"";
                                            
                                            
                                            self.currencyRelationTwoLabel.text=@"";
                                            self.priceTwoLabel.text=@"";
                                            self.upsAndDownsTwoLabel.text=@"";
                                            
                                            
                                            self.currencyRelationThreeLabel.text=@"";
                                            self.priceThreeLabel.text=@"";
                                            self.upsAndDownsThreeLabel.text=@"";
                                        }
                                        
                                    } else {
                                        self.currencyRelationOneLabel.text=@"";
                                        self.priceOneLabel.text=@"";
                                        self.upsAndDownsOneLabel.text=@"";
                                        
                                        
                                        self.currencyRelationTwoLabel.text=@"";
                                        self.priceTwoLabel.text=@"";
                                        self.upsAndDownsTwoLabel.text=@"";
                                        
                                        
                                        self.currencyRelationThreeLabel.text=@"";
                                        self.priceThreeLabel.text=@"";
                                        self.upsAndDownsThreeLabel.text=@"";
                                        [MBProgressHUD showError:responseData[@"msg"]];
                                    }
                                }
                                failure:^(NSError *error) {
                                    self.currencyRelationOneLabel.text=@"";
                                    self.priceOneLabel.text=@"";
                                    self.upsAndDownsOneLabel.text=@"";
                                    
                                    
                                    self.currencyRelationTwoLabel.text=@"";
                                    self.priceTwoLabel.text=@"";
                                    self.upsAndDownsTwoLabel.text=@"";
                                    
                                    
                                    self.currencyRelationThreeLabel.text=@"";
                                    self.priceThreeLabel.text=@"";
                                    self.upsAndDownsThreeLabel.text=@"";
                                }
                                showHUD:NO];
}



#pragma mark - SDCycleScrollViewDelegate

- (void)cycleScrollView:(SDCycleScrollView *)cycleScrollView didSelectItemAtIndex:(NSInteger)index
{
    NSLog(@"---点击了第%ld张图片", (long)index);
    
    
}




- (IBAction)change {
    
    self.fallBtn.selected=!self.fallBtn.selected;
    self.riseBtn.selected=NO;
    [UIView animateWithDuration:0.2 animations:^{
        self.lineView.transform = CGAffineTransformMakeTranslation(90, 0);
        self.lineView.backgroundColor=RGBColor(225, 69, 79);
    }];
    [self.tableView reloadData];
}
- (IBAction)changAction {
    self.riseBtn.selected=!self.riseBtn.selected;
    self.fallBtn.selected=NO;
    [UIView animateWithDuration:0.2 animations:^{
        self.lineView.transform = CGAffineTransformMakeTranslation(0, 0);
        self.lineView.backgroundColor=RGBColor(44, 178, 90);
    }];
    [self.tableView reloadData];
}
- (IBAction)changeEyeAction:(UIButton *)sender {
    sender.selected = !sender.selected;
    if (sender.selected) {
        self.hidePropertyLabel.hidden=NO;
        self.propertyLabel.hidden=YES;
    }else{
        self.hidePropertyLabel.hidden=YES;
        self.propertyLabel.hidden=NO;
    }
    
    
}




- (void)viewWillAppear:(BOOL)animated {
    [super viewWillAppear:animated];
    if ([[UserDefaults objectForKey:K_PRICE] isEqualToString:@"1"]){
        self.hidePropertyLabel.text=@"***BTC ≈ ***CNY";
    }else if ([[UserDefaults objectForKey:K_PRICE] isEqualToString:@"2"]){
        self.propertyLabel.text=@"***BTC ≈ ***USD";
    }else{
        self.propertyLabel.text=@"***BTC ≈ ***AUD";
    }
    [self.navigationController setNavigationBarHidden:YES animated:NO];
    [self setStatusBarBackgroundColor:RGBColor(46, 49, 69)];
    if ([UserDefaults objectForKey:K_LOGININFO_TOKEN]) {
        self.headerV.frame=CGRectMake(0, 0, ScreenWidth, 498);
        [self.tableView setTableHeaderView:self.headerV];
        self.contentView.hidden=NO;
        self.loginView.hidden=YES;
        // 我的资产
        [self requstMoneyData];
    }else{
        self.headerV.frame=CGRectMake(0, 0, ScreenWidth, 470);
        [self.tableView setTableHeaderView:self.headerV];
        self.contentView.hidden=YES;
        self.loginView.hidden=NO;
    }
    
    
    
    
}

-(void)requstMoneyData{
    
    [LSRequestManager getRequestWithURL:HUOBI(@"api/currency/myAccount")
                                 params:nil
                                success:^(NSDictionary *responseData) {
                                    if ([responseData[@"ret"] integerValue ] == 0) {
                                        
                                        if ([[UserDefaults objectForKey:K_PRICE] isEqualToString:@"1"]){
                                            self.propertyLabel.text=[NSString stringWithFormat:@"%@ ≈ %@",responseData[@"data"][@"totalAmountBTC"],responseData[@"data"][@"totalAmountCNY"]];
                                        }else if ([[UserDefaults objectForKey:K_PRICE] isEqualToString:@"2"]){
                                            self.propertyLabel.text=[NSString stringWithFormat:@"%@ ≈ %@",responseData[@"data"][@"totalAmountBTC"],responseData[@"data"][@"totalAmountUSD"]];
                                        }else{
                                            self.propertyLabel.text=[NSString stringWithFormat:@"%@ ≈ %@",responseData[@"data"][@"totalAmountBTC"],responseData[@"data"][@"totalAmountAUD"]];
                                        }
                                    } else {
                                        self.propertyLabel.text=@"";
                                        [MBProgressHUD showError:responseData[@"msg"]];
                                    }
                                }
                                failure:^(NSError *error) {
                                    self.propertyLabel.text=@"";
                                }
                                showHUD:NO];
    
    
    
    
    
    
}
- (void)setStatusBarBackgroundColor:(UIColor *)color {
    
    UIView *statusBar = [[[UIApplication sharedApplication] valueForKey:@"statusBarWindow"] valueForKey:@"statusBar"];
    if ([statusBar respondsToSelector:@selector(setBackgroundColor:)]) {
        statusBar.backgroundColor = color;
    }
}
- (void)viewWillDisappear:(BOOL)animated {
    [super viewWillDisappear:animated];
    
    [self.navigationController setNavigationBarHidden:NO animated:NO];
    [self setStatusBarBackgroundColor:[UIColor clearColor]];
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}


-(void)loadNewData{
    
    [LSRequestManager getRequestWithURL:HUOBI(@"api/pub/slideImg")
                                  params:nil
                                 success:^(NSDictionary *responseData) {
                                     if ([responseData[@"ret"] integerValue ] == 0) {
                                         [self.bannerArray removeAllObjects];
                                         [self.bannerArray addObjectsFromArray: [BannerModel mj_objectArrayWithKeyValuesArray:responseData[@"data"]]];
                                         [self setAd];
                                     } else {
                                         [MBProgressHUD showError:responseData[@"msg"]];
                                     }
                                 }
                                 failure:^(NSError *error) {
                                     
                                 }
                                 showHUD:NO];
}
- (void)setAd {
    NSMutableArray *array = [NSMutableArray array];
    for (BannerModel *bannerModel in self.bannerArray) {
        
        
        [array addObject:bannerModel.path];
    }
    [self.bannerScrollView setImageURLStringsGroup:array];
}



#pragma mark - Table view data source

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {

    return 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {

    return 10;
}
-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    return 60;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    HomeTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"HomeTableViewCell" forIndexPath:indexPath];
    if (self.riseBtn.selected) {
        cell.isRiseLabel.backgroundColor=RGBColor(44, 178, 90);
    }else{
        cell.isRiseLabel.backgroundColor=RGBColor(225, 69, 79);
    }
    
    
    
    return cell;
}




- (IBAction)loginAction {
    
    
    BaseNavigationController *base = [[UIStoryboard storyboardWithName:@"Login" bundle:nil] instantiateInitialViewController];
    [self presentViewController:base animated:NO completion:nil];

}
@end
